package apresentacao.caminhoneiro;

import java.util.ArrayList;
import java.util.HashMap;

import com.example.caminhoneiro.R;

import controle.caminhoneiro.Controller_Tela_Inicial;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnFocusChangeListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class Tela_Inicial extends Activity implements OnClickListener, OnFocusChangeListener {

	Button btEntrar, btNovaConta;
	TextView tvEsqueci_Senha;
	EditText etCPF, etSenha;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.tela_inicial);
		inicializar();
	}

	public void mensagem(String m){
		Toast.makeText(this, m, Toast.LENGTH_LONG).show();
	}
	public void inicializar() {
		btEntrar = (Button) findViewById(R.id.Tela_Inicial_BtEntrar);
		btEntrar.setOnClickListener(this);

		btNovaConta = (Button) findViewById(R.id.Tela_Inicial_BtNova_Conta);
		btNovaConta.setOnClickListener(this);

		tvEsqueci_Senha = (TextView) findViewById(R.id.Tela_Inicial_TvEsqueci_Senha);
		tvEsqueci_Senha.setOnClickListener(this);

		etCPF = (EditText) findViewById(R.id.Tela_Inicial_EtCPF);
		etCPF.setOnFocusChangeListener(this);
		
		etSenha = (EditText) findViewById(R.id.Tela_Inicial_EtSenha);
		etSenha.setOnFocusChangeListener(this);
	}

	@Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
		if (v.getId() == btEntrar.getId()) {
			String CPF 		= etCPF.getText().toString();
			String senha 	= etSenha.getText().toString();
//			HashMap<String, String> hash = new Controller_Tela_Inicial().verifica_Login(CPF, senha);
			HashMap<String, String> hash = new HashMap<String, String>();
			if(hash.size() == 0){
				Intent it = new Intent(this,Activity_Menu_Principal.class);
				startActivity(it);
//				new Controller_Tela_Inicial().login(CPF, senha, this);
			}else{
					ArrayList<String> list = new ArrayList<String>();
					String CPF_Hash = hash.get("CPF");
					if(CPF_Hash != null){
						list.add(CPF_Hash);
					}
					String senha_Hash = hash.get("senha");
					if(senha_Hash != null){
						list.add(senha_Hash);
					}
					String erros[] = new String[list.size()];
					for(int i = 0; i <erros.length; i++){
						erros[i] = list.get(i);
					}
					ArrayAdapter<String> array = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, erros);
					AlertDialog.Builder alert = new AlertDialog.Builder(this);
					alert.setAdapter(array, null);
					alert.setNegativeButton(android.R.string.ok, new DialogInterface.OnClickListener() {
			
						@Override
						public void onClick(DialogInterface dialog, int which) {
							// TODO Auto-generated method stub
							dialog.cancel();
						}
					});
					alert.setTitle(this.getString(R.string.erro));
					AlertDialog myAlert = alert.create();
					myAlert.show();
			}
		} else if (v.getId() == tvEsqueci_Senha.getId()) {
			Intent it = new Intent(this, Esqueci_Senha.class);
			startActivity(it);

		} else if (v.getId() == btNovaConta.getId()) {
			Intent it = new Intent(this, Nova_Conta.class);
			startActivity(it);
		}
	}

	@Override
	public void onFocusChange(View event, boolean hasFocus) {
		// TODO Auto-generated method stub
		EditText edit = (EditText) event;
		// TODO Auto-generated method stub
		switch (event.getId()) {
			case R.id.Tela_Inicial_EtCPF:{
				if(hasFocus && etCPF.getText().toString().equals(this.getString(R.string.cpf))){
					edit.setText("");
				}
				else if(!hasFocus && etCPF.getText().toString().equals("")){ 
					edit.setText(this.getString(R.string.cpf));
				}
				break;
			}
			case R.id.Tela_Inicial_EtSenha:{
				if(hasFocus && etSenha.getText().toString().equals(this.getString(R.string.senha))){
					edit.setText("");
					edit.setTransformationMethod(PasswordTransformationMethod.getInstance());
				}
				else if(!hasFocus && etSenha.getText().toString().equals("")){ 
					edit.setText(this.getString(R.string.senha));
					edit.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
				}
				break;
			}
		}
	}
}